/*!***************************************************
 * @file: func.h
 * @brief: 具体功能实现头文件
 * @author: ITHAO 
 * @date: 2018.12.7
 * @note: 
 ****************************************************/
#ifndef __FUNC_H__
#define __FUNC_H__

#include <stdbool.h>

#define UNSEND_CHAT_PACKET_FILE "./log/unsend_chat_packet.txt"
#define TEMP_FILE               "./log/tmp.txt"

#define FRIEND_INFO_FILE        "./log/friend.txt"
#define LOG_DIR                 "./log"

enum {
    FUNC_NO_OWNER_INFO= 2,
    FUNC_EXCEED_FRIEND_NUM,
    FUNC_NOT_FRIEND,
    FUNC_IS_FRIEND_ALREADY,
};

enum {
    FUNC_PARAM_NULL_ERR = 1,
    FUNC_FD_INVALID_ERR,
    FUNC_OPEN_FILE_FAIL_ERR,
    FUNC_SAVE_INFO_ERR,
};


#define NAME_MAX_LENGTH     11
#define ACCOUNT_MAX_LENGTH  11
#define MAX_FRIEND_NUM      20
/*friend_info用于存放一个好友的信息*/
struct friend_info {
    bool used_flag;/*是否被使用标志*/
    char name[NAME_MAX_LENGTH];
    char account[ACCOUNT_MAX_LENGTH];
};

/*friend结构体是用于文件读写*/
struct friend {
    char owner_account[ACCOUNT_MAX_LENGTH];
    unsigned int friend_num;
    struct friend_info info[MAX_FRIEND_NUM];
};


/*长度限制 =
为无符号16位长度 -
发送者账号长度和时间数组长度 -
数据打包时的命令类型、命令、ack*/
#define MSG_MAX_LENGTH (65535-48-3)
struct chat_packet {
    char send_name[NAME_MAX_LENGTH];
    char send_account[ACCOUNT_MAX_LENGTH];
    char recv_account[ACCOUNT_MAX_LENGTH];
    char time[26];
    char msg[MSG_MAX_LENGTH];
};

extern bool load_friend(char *account, struct friend *p_friend);
extern int device_add_friend(struct friend_info new_friend, char *owner_account);
extern int device_remove_friend(char *friend_account, char *owner_account);

extern void init_chat_packet(struct chat_packet *p_chat_pakcet);
extern bool save_chat_packet(const struct chat_packet *p_chat_pakcet);
extern bool serch_chat_packet(char *recv_account,
                      struct chat_packet *p_chat_pakcet);

#endif